<template>
  <el-dialog :custom-class="'user-editor'" title="新建用户" :visible="visible" @close="close">
    <el-form :model="form" :rules="rules" ref="ruleForm" class="user-ruleForm">
      <el-form-item label="用户名" prop="username" :label-width="formLabelWidth">
        <el-input v-model="form.username" autocomplete="off" placeholder="请输入用户名"></el-input>
      </el-form-item>
      <el-form-item label="密码" prop="password" :label-width="formLabelWidth">
        <el-input v-model="form.password" autocomplete="off"></el-input>
      </el-form-item>
      <el-form-item label="角色" prop="role" :label-width="formLabelWidth">
        <el-select v-model="form.role" placeholder="请选择用户角色">
          <el-option label="普通用户" :value="0"></el-option>
          <el-option label="管理员" :value="1"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="昵称" prop="real_name" :label-width="formLabelWidth">
        <el-input v-model="form.real_name" autocomplete="off" placeholder="请输入昵称"></el-input>
      </el-form-item>
      <el-form-item label="邮箱" :label-width="formLabelWidth">
        <el-input v-model="form.email" autocomplete="off" placeholder="请输入邮箱"></el-input>
      </el-form-item>
      <el-form-item label="电话" :label-width="formLabelWidth">
        <el-input v-model="form.mobile" autocomplete="off" placeholder="请输入电话"></el-input>
      </el-form-item>
      <el-form-item label="备注" :label-width="formLabelWidth">
        <el-input v-model="form.remark" autocomplete="off" placeholder="请输入备注"></el-input>
      </el-form-item>
      <el-form-item label="头像" :label-width="formLabelWidth">
        <el-input v-model="form.pic" autocomplete="off" placeholder="请输入头像地址"></el-input>
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button @click="close">取 消</el-button>
      <el-button type="primary" @click="submit">确 定</el-button>
    </div>
  </el-dialog>
</template>

<script>
import { createNamespacedHelpers } from "vuex";
const { mapActions } = createNamespacedHelpers("admin");

export default {
  props: {
    visible: {
      type: Boolean
    }
  },
  data() {
    return {
      formLabelWidth: "80px",
      form: {
        username: null,
        password: null,
        role: null,
        real_name: null,
        email: null,
        mobile: null,
        remark: null,
        pic: null
      },
      rules: {
        username: [
          { required: true, message: "请输入用户名", trigger: "change" }
        ],
        real_name: [
          { required: true, message: "请输入昵称", trigger: "change" }
        ],
        password: [
          { required: true, message: "请输入密码", trigger: "change" }
        ],
        role: [{ required: true, message: "请输入角色", trigger: "change" }]
      }
    };
  },
  methods: {
    ...mapActions(["addUser"]),
    submit() {
      this.$refs.ruleForm.validate(valid => {
        if (valid) {
          this.handleCreateUser();
        } else {
          return false;
        }
      });
    },
    handleCreateUser() {
      this.addUser(this.form).then(() => {
        this.$message({
          type: "success",
          message: "提交成功"
        });
        this.close();
      });
    },
    close() {
      this.$emit("close");
    }
  }
};
</script>

<style lang="scss" scoped>
/deep/.user-editor {
  width: 600px;
  .el-dialog__body {
    padding: 24px 48px;
    .el-select {
      width: 100%;
    }
  }
}
</style>